package com.qiezi;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;


public class Hook implements IXposedHookLoadPackage {

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (lpparam.packageName.equals("com.live.eggplant")){
            XposedBridge.log("发现包名开始hook："+lpparam.packageName);
            XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class,new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    ClassLoader classLoader = ((Context) param.args[0]).getClassLoader();
                    Class<?> hookclass = null;
                    try {
                        hookclass = classLoader.loadClass("com.live.eggplant.base.helper.SPHelper");
                    } catch (Exception e) {
                        return;
                    }
                    //找到类了开始hook
                    XposedHelpers.findAndHookMethod("com.live.eggplant.base.helper.SPHelper",
                            classLoader, "saveObject",
                            Context.class, String.class, Object.class,
                            new XC_MethodHook() {
                                @Override
                                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                    super.beforeHookedMethod(param);
                                    XposedBridge.log("-----调用saveObject-----"+param.args[1]);
                                    if (param.args[1].equals("SP_BootStrap")){
                                        XposedBridge.log("BootStrap参数isVipUser为："+
                                                XposedHelpers.getBooleanField(param.args[2],"isVipUser"));
                                        XposedHelpers.setBooleanField(param.args[2],"isVipUser",true);
                                    }else if (param.args[1].equals("user_info")){
                                        XposedBridge.log("User参数is_vip为："+
                                                XposedHelpers.getBooleanField(param.args[2],"is_vip"));
                                        XposedHelpers.setBooleanField(param.args[2],"is_vip",true);
                                        Object userlevel=XposedHelpers.getObjectField(param.args[2],"level");
                                        XposedBridge.log("UserLevel参数views_number为："+
                                                XposedHelpers.getIntField(userlevel,"views_number"));
                                        XposedHelpers.setIntField(userlevel,"views_number",999);
                                        XposedHelpers.setIntField(userlevel,"download_times",666);
                                        XposedHelpers.setObjectField(param.args[2],"level",userlevel);
                                    }else if (param.args[1].toString().contains("ads")){
                                        param.args[2]=null;
                                    }

                                }

                                @Override
                                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                    super.afterHookedMethod(param);
                                    if (param.args[1].equals("SP_BootStrap")){
                                        XposedBridge.log("BootStrap参数isVipUser为："+
                                                XposedHelpers.getBooleanField(param.args[2],"isVipUser"));
                                    }
                                }
                            }
                    );
                    //找到类了开始getobject
                    XposedHelpers.findAndHookMethod("com.live.eggplant.base.helper.SPHelper",
                            classLoader, "getObject",
                            Context.class, String.class,
                            new XC_MethodHook() {
                                @Override
                                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                                    super.beforeHookedMethod(param);
                                }

                                @Override
                                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                                    super.afterHookedMethod(param);
                                    if (param.args[1].toString().contains("ads")){
                                        XposedBridge.log("包含 ads 的有："+param.args[1]);
                                        param.setResult(null);
                                    }
                                }
                            }
                    );
                }
            });

        }
    }
}
